/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab-modules.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

//===========================================================================================
#include "SimObjHanger.h"

//===========================================================================================
const dylab::String dylab::SimObjHanger::COMPONENT_NAME = "simObjHanger";

//===========================================================================================
dylab::SimObjHanger::SimObjHanger()
: ComEventListener(COMPONENT_NAME), mSimulation(NULL), mTargetPos(Vector3::ZERO)
, mForceRadius(32), mForceCoeff(100), mForceAttenuationExp(DYLAB_R(0.5))
, mSourceSimObj(NULL)
{
}
//-----------------------------------------------------------------------------
dylab::SimObjHanger::~SimObjHanger()
{
}
//-----------------------------------------------------------------------------
void dylab::SimObjHanger::initialize(Simulation * _simulation)
{ 
    // store the _simulation pointer
    mSimulation = _simulation; 

    // register our listener
    registerAsInputEventListener();
    registerAsGfxEventListener();
}
//-----------------------------------------------------------------------------
dylab::Simulation * dylab::SimObjHanger::getParentSimulation()
{
    return mSimulation;
}
//-----------------------------------------------------------------------------
const dylab::Simulation * dylab::SimObjHanger::getParentSimulation() const
{
    return mSimulation;
}
//-----------------------------------------------------------------------------
void dylab::SimObjHanger::loadFromPropertyTree(const PropertyTree & _pt)
{
	mForceRadius = _pt.getNodeValueT("forceRadius", mForceRadius);
	mForceCoeff = _pt.getNodeValueT("forceCoeff", mForceCoeff);
	mForceAttenuationExp = _pt.getNodeValueT("forceAttenExp", mForceAttenuationExp);
}
//-----------------------------------------------------------------------------
void dylab::SimObjHanger::processEvent(const InputEvent & _inputEvent)
{                     
    switch (_inputEvent.getType())                        
    {
    case InputEvent::MOUSE_DOWN:        
        {
            ComGfx::IScene::ScopedExLock scene(mSimulation->getGfx());
            ComGfx::IRayQuery::IResult * result 
                = scene->getRayQuery("simObjPicker")->execute(_inputEvent.getMouseAbsX(), _inputEvent.getMouseAbsY(), &mTargetPos);
            if (result != NULL) // intersection was found
            {
                mSourceSimObj = dynamicCast<ComGfx::IModelRenderer>(result)->getSimObject();                
                mSourceMassPointId = ComModelStructure::IMassPoints::ScopedExLock(mSourceSimObj->getModelStructure())->getNearestMassPointId(mTargetPos);
            }
        }

        
        break;
    case InputEvent::MOUSE_UP:
        mSourceSimObj = NULL;
        break;
    case InputEvent::MOUSE_MOVE:
        if (mSourceSimObj != NULL)
        {
            //mTargetPos = ComGfx::ICamera::ScopedShLock(mSimulation->getGfx())
              //  ->projectScreenCoords(_inputEvent.getMouseAbsX(), _inputEvent.getMouseAbsY(), mTargetPos);
        }
        break;
    }
}
//-----------------------------------------------------------------------------
void dylab::SimObjHanger::processEvent(const GfxEvent & _gfxEvent)
{
    if (mSourceSimObj != NULL)
    {
		ComModelStructure::IMassPoints::ScopedExLock massPoints(mSourceSimObj->getModelStructure());
        massPoints->applyPointMagnetForce(mSourceMassPointId, mForceRadius, 
			_gfxEvent.getFrameDurationMS() * mForceCoeff, mForceAttenuationExp);
    }
}
//-----------------------------------------------------------------------------